<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Admin\Actions\Row\MemberTopoupOrderApproveAction;
use App\Admin\Actions\Row\MemberTopoupOrderRejectAction;
use App\Admin\Repositories\MemberTopupOrder;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class MemberTopupOrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new MemberTopupOrder(['user']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('order_no');
            $grid->column('user.name', '会员名');
            $grid->column('user.flag', '会员标签');
            $grid->column('user.telephone', '会员电话')->copyable();
            $grid->column('amount');
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                // 移除默认操作
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->disableQuickEdit();
                $actions->disableView();

                // 仅当状态为待审核时显示按钮
                if ($actions->row->review === 0) {
                    $actions->append(new MemberTopoupOrderApproveAction());
                    $actions->append(new MemberTopoupOrderRejectAction());
                }
            });
            $grid->column('payment_gateway', '渠道')->using([
                1 => '支付宝',
                2 => '微信',
                3 => '银行卡',
            ]);
            $grid->column('certificate')->image('', 50, 50);
            $grid->column('message', '消息')->editable();
            $grid->column('status', '支付状态')->using([
                0 => '待支付',
                1 => '已支付',
                2 => '已取消',
            ]);
            $grid->column('review', '审核状态')->using([
                0 => '待审核',
                1 => '已审核',
                2 => '已驳回',
            ]);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->export();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('order_no');
                $filter->equal('user.name', '会员名称');
                $filter->like('user.telephone', '会员电话');
                $filter->equal('user.flag', '会员标签');
                $filter->equal('review', '审核状态')->select([
                    0 => '待审核',
                    1 => '已审核',
                    2 => '已驳回',
                ]);
                $filter->between('created_at', '创建时间')->datetime();
                $filter->between('updated_at', '修改时间')->datetime();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new MemberTopupOrder(), function (Show $show) {
            $show->field('id');
            $show->field('order_no');
            $show->field('user_id');
            $show->field('amount');
            $show->field('payment_gateway');
            $show->field('certificate');
            $show->field('remark');
            $show->field('status');
            $show->field('review');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new MemberTopupOrder(), function (Form $form) {
            $form->display('id');
            $form->text('order_no');
            $form->text('user_id');
            $form->text('amount');
            $form->text('payment_gateway');
            $form->image('certificate')->autoUpload();
            $form->text('message', '消息');
            $form->text('status');
            $form->text('review');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
